package com.kzd.sdk.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kzd.sdk.base.OwnerActivity;
import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.entity.KZD_UserInfo;
import com.kzd.sdk.utils.AppUtils;

public class KZD_UserPhoneActivity extends OwnerActivity {

    public static void open(Context context) {
        Intent intent = new Intent(context, KZD_UserPhoneActivity.class);
        context.startActivity(intent);
    }

    private EditText etUserPhoneMobile, etUserPhoneCode;
    private TextView tvUserPhoneGetCode,tvUserPhoneSubmit;

    private String mSessionId = "";

    private final CountDownTimer mCountDownTimer = new CountDownTimer(1000 * 60, 1000) {
        public void onTick(long millisUntilFinished) {
            String text = "剩余" + millisUntilFinished / 1000 + "秒";
            tvUserPhoneGetCode.setText(text);
        }

        public void onFinish() {
            tvUserPhoneGetCode.setTextColor(Color.parseColor("#2983FF"));
            tvUserPhoneGetCode.setEnabled(true);
            tvUserPhoneGetCode.setText("获取验证码");
        }
    };

    private final KZD_HttpCallback<String> callbackPhoneCode = new KZD_HttpCallback<String>() {
        @Override
        public void onSuccess(String data) {
            mSessionId = data;
            mCountDownTimer.start();
            tvUserPhoneGetCode.setTextColor(Color.parseColor("#999999"));
            tvUserPhoneGetCode.setEnabled(false);
        }

        @Override
        public void onError(String error) {
            toast(error);
        }
    };

    private final KZD_HttpCallback<KZD_UserInfo> httpListenerPhoneBind = new KZD_HttpCallback<KZD_UserInfo>() {
        @Override
        public void onSuccess(KZD_UserInfo data) {
            toast("绑定成功");
            finish();
        }

        @Override
        public void onError(String error) {
            toast(error);
        }
    };

    @Override
    public String getLayoutIdByName() {
        return "kzd_activity_user_phone";
    }

    @Override
    public View getLayoutView() {
        return null;
    }

    @Override
    public void initView() {
        etUserPhoneMobile = findViewById(AppUtils.getId((this), "etUserPhoneMobile"));
        etUserPhoneCode = findViewById(AppUtils.getId((this), "etUserPhoneCode"));
        tvUserPhoneGetCode = findViewById(AppUtils.getId((this), "tvUserPhoneGetCode"));
        tvUserPhoneSubmit = findViewById(AppUtils.getId((this), "tvUserPhoneSubmit"));

        findViewById(AppUtils.getId((this),"ivBackPressed"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });

        tvUserPhoneGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = etUserPhoneMobile.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    toast("请输入手机号");
                } else {
                    KZD_ApiManager.httpPhoneCode(phone, 0, callbackPhoneCode);
                }
            }
        });

        tvUserPhoneSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = etUserPhoneMobile.getText().toString();
                String code = etUserPhoneCode.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    toast("请输入手机号");
                } else if (TextUtils.isEmpty(code)) {
                    toast("请输入验证码");
                } else {
                    KZD_ApiManager.httpPhoneBind(phone, mSessionId, code, httpListenerPhoneBind);
                }
            }
        });
    }

    @Override
    public void initData() {
    }

    @Override
    protected void onStop() {
        super.onStop();
        mCountDownTimer.cancel();
    }
}
